import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';

/**
 * 选择蓝牙弹窗
 */
Future<Map<String, dynamic>?> showBluetoothListDialog(
    List<Map<String, dynamic>> list) {
  return SmartDialog.show(
      alignment: Alignment.center,
      builder: (context) => _BluetoothWidget(list));
}

class _BluetoothWidget extends StatefulWidget {
  final List<Map<String, dynamic>> list;

  const _BluetoothWidget(this.list, {super.key});

  @override
  State<_BluetoothWidget> createState() => _BluetoothWidgetState();
}

class _BluetoothWidgetState extends State<_BluetoothWidget> {
  @override
  Widget build(BuildContext context) {
    return Container(
        width: 600,
        height: 260,
        decoration: BoxDecoration(
          borderRadius: const BorderRadius.all(Radius.circular(20.0)),
          color: Colors.white,
        ),
        child: Column(
          children: [
            Container(
              padding: EdgeInsets.all(10),
              child: Center(
                child: Text(
                  '请选择蓝牙设备',
                  style: TextStyle(fontSize: 18),
                ),
              ),
            ),
            Expanded(
              child: ListView.separated(
                  itemBuilder: (context, index) {
                    return InkWell(
                      onTap: () {
                        print("点击了 $index");
                        SmartDialog.dismiss(result: widget.list[index]);
                      },
                      child: Container(
                        padding: const EdgeInsets.only(top: 10, bottom: 10),
                        child: Center(
                          child: Text(widget.list[index]["mBleName"]),
                        ),
                      ),
                    );
                  },
                  separatorBuilder: (context, index) => Divider(
                        thickness: 1,
                        height: 1,
                        color: Colors.grey,
                      ),
                  itemCount: widget.list.length),
            )
          ],
        ));
  }
}
